package edu.gyc.hiblog.config;

import edu.gyc.hiblog.model.Category;
import edu.gyc.hiblog.service.CategoryService;
import edu.gyc.hiblog.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;
@Component

public class ContextStartup implements ApplicationRunner, ServletContextAware {
    @Autowired
    CategoryService categoryService;

    ServletContext servletContext;

    @Autowired
    PostService postService;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<Category> categories=categoryService.lambdaQuery().eq(Category::getStatus,0).list();
        servletContext.setAttribute("categories",categories);
        postService.initWeekRank();
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
